package com.ytjj.qmyx.mall.service;

import com.alipay.api.response.AlipayTradeQueryResponse;
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.wxpay.sdk.WXPay;
import com.kuaidi100.sdk.response.QueryTrackData;
import com.kuaidi100.sdk.response.QueryTrackResp;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.qmyx.mall.model.FreightTemplateResponse;
import com.ytjj.qmyx.mall.model.Orders;
import com.ytjj.qmyx.mall.model.OrdersInfo;
import com.ytjj.qmyx.mall.model.Product;
import com.ytjj.qmyx.mall.model.request.*;
import com.ytjj.qmyx.mall.model.response.*;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 订单服务接口
 *
 *
 * @version 1.0
 *
 */
public interface OrdersService {

    void rechargeUpdate();


    void addTask(Integer orderId);

    /**
     * 创建订单
     *
     * @param request
     * @return
     */
    Integer createOrder(CreateOrderRequest request);

    /**
     * 购物车创建订单
     * @param request
     * @return
     */
    Integer cartCreateOrder(CreateOrderRequest request);

    /**
     * 保存订单
     *
     * @param userId
     * @param tel
     * @param originalPrice
     * @param price
     * @param orderSource
     * @return
     */
    Orders saveOrders(Integer userId, String tel, BigDecimal originalPrice, BigDecimal price,
                      String orderSource,
                      String channel,Integer isHelpOrder);

    /**
     * 保存订单
     *
     * @param userId
     * @param tel
     * @param originalPrice
     * @param price
     * @param orderSource
     * @return
     */
    Orders saveOrders(Integer userId, String tel, BigDecimal originalPrice, BigDecimal price,
                      String orderSource,
                      String channel,BigDecimal deduction,Integer dynamicId, String orderParentNo,String account);

    /**
     * 保存订单
     *
     * @param userId
     * @param tel
     * @param originalPrice
     * @param price
     * @param orderSource
     * @param orderParentNo 组合订单编号
     * @return
     */
    Orders saveOrders(Integer userId, String tel, BigDecimal originalPrice, BigDecimal price,
                      String orderSource,
                      String channel,Integer isHelpOrder, String orderParentNo);

    /**
     * 保存订单
     *
     * @param userId
     * @param tel
     * @param address
     * @param userName
     * @param originalPrice
     * @param price
     * @param orderSource
     * @param domainUrl 下单域名
     * @return
     */
    Orders saveOrders(Integer userId, String tel,String province,String city,String area, String address,
                      String userName, BigDecimal originalPrice, BigDecimal price,
                      String orderSource, String channel, String h5token, String orderParentNo,String domainUrl,String orderPlatform,BigDecimal deductionMoney);

    /**
     * 保存订单明细
     *
     * @param orderId
     * @param productId
     * @param productName
     * @param productImg
     * @param price
     * @param originalPrice
     * @param warehouse
     * @param warehouseName
     * @param warehouseRule
     * @param specialId
     * @param skuSpec
     * @param bankerId
     * @param specId
     * @return
     */
//    OrdersInfo saveOrdersInfo(Integer orderId, Integer productId, String productName, String productImg,
//                              BigDecimal price, BigDecimal originalPrice, String warehouse,
//                              String warehouseName, String warehouseRule, Integer specialId,
//                              String skuSpec, Integer bankerId, Integer specId, String activityType,
//                              Integer activityId, BigDecimal activityMax, BigDecimal activityMin,
//                              String descr,Integer isDirectBuy,String pageSource,Integer evaluationId,
//                              Integer productType,Integer isHelpOrder);
    OrdersInfo saveOrdersInfo(Integer orderId, Integer productId, String productName, String productImg,
                              BigDecimal price, BigDecimal originalPrice, String warehouse,
                              String warehouseName, String warehouseRule, Integer specialId,
                              String skuSpec, Integer bankerId, Integer specId, String activityType,
                              Integer activityId, BigDecimal activityMax, BigDecimal activityMin,
                              String descr,Integer isDirectBuy,String pageSource,Integer evaluationId,
                              Integer productType,Integer isHelpOrder, BigDecimal costPrice,Integer productSum,String two);
    OrdersInfo saveOrdersInfo(Integer orderId, Integer productId, String productName, String productImg,
                              BigDecimal price, BigDecimal originalPrice, String warehouse,
                              String warehouseName, String warehouseRule, Integer specialId,
                              String skuSpec, Integer bankerId, Integer specId, String activityType,
                              Integer activityId, BigDecimal activityMax, BigDecimal activityMin,
                              String descr,Integer isDirectBuy,String pageSource,Integer evaluationId,
                              Integer productType,Integer isHelpOrder, BigDecimal costPrice,Integer productSum);
    OrdersInfo saveOrdersInfo(Integer orderId, Integer productId, String productName, String productImg,
                              BigDecimal price, BigDecimal originalPrice, String warehouse,
                              String warehouseName, String warehouseRule, Integer specialId,
                              String skuSpec, Integer bankerId, Integer specId, String activityType,
                              Integer activityId, BigDecimal activityMax, BigDecimal activityMin,
                              String descr,Integer isDirectBuy,String pageSource,Integer evaluationId,
                              Integer productType,Integer isHelpOrder, BigDecimal costPrice, String threeSkuId,Integer productSum,String type_one,String scene);
    OrdersInfo saveOrdersInfo(Integer orderId, Integer productId, String productName, String productImg,
                              BigDecimal price, BigDecimal originalPrice, String warehouse,
                              String warehouseName, String warehouseRule, Integer specialId,
                              String skuSpec, Integer bankerId, Integer specId, String activityType,
                              Integer activityId, BigDecimal activityMax, BigDecimal activityMin,
                              String descr,Integer isDirectBuy,String pageSource,Integer evaluationId,
                              Integer productType,Integer isHelpOrder, BigDecimal costPrice, String threeSkuId,Integer AppraisalType);

    /**
     * 微信app预付款下单接口
     *
     * @param request
     * @return
     */
    Map wxDoUnifiedOrder(UnifiedOrderRequest request);

    Map wxDoUnifiedOrderV2(UnifiedOrderRequest request);

    Map balancePayByOrder(UnifiedOrderRequest request);

    /**
     * 根据订单id获取订单--【详情】--信息
     * @param orderInfoId
     * @return
     */
    OrderResponse queryOrderInfoById(Integer orderInfoId, String source,UsersResponse usersResponse);

    OrderResponse queryOrderInfoByChildOrderNo(String childOrderNo, String source, UsersResponse usersResponse);

    /**
     * 运算运费
     */
    BigDecimal calculateFreight(FreightTemplateResponse freightTemplateResponse,Integer productNum, String city,String province);

    /**
     * 获取运费
     * @param orderId
     * @param city
     * @return
     */
    BigDecimal getCalculateFreight(Integer orderId, String city,String province);

    /**
     * 根据详情id查询物流信息
     * @param orderInfoId
     * @return
     */
    List<QueryTrackData> getLogisticsInfo(Integer orderInfoId);

    /**
     * 收货
     * @param orderInfoId
     */
    void receivingGoods(Integer orderInfoId);

    /**
     * 根据订单id获取订单--【支付】--信息
     *
     * @param orderId
     * @return
     */
    OrderResponse getOrderInfoById(Integer orderId, Integer userId,String city);

    OrderResponse getOrderInfoByNo(String orderNo,Integer userId,String city);

    /**
     * 根据订单id获取订单信息
     */

    /**
     * 支付成功后返回的信息
     * @param orderId
     * @param usersResponse
     * @return
     */
    OrderResponse getPaySuccessInfo(Integer orderId, UsersResponse usersResponse,Integer payOrderId);


    OrderResponse getPaySuccessInfoByOrderNo(String orderNo, UsersResponse usersResponse,Integer payOrderId);

    /**
     * 支付成功页面领取红包
     * @param orderId
     * @param userId
     * @return
     */
    void receiveReward(Integer orderId, Integer userId);

    /**
     * 编辑支付订单的用户信息
     * @param ordersUserInfoRequest
     */
    void updateOrdersUserInfo(OrdersUserInfoRequest ordersUserInfoRequest);


    /**
     * 微信app回调
     *
     * @param resXml
     * @return
     */
    String wxAppPayBack(String resXml);

    /**
     * 更新订单状态
     *
     * @param map
     */
    void updateOrderPay(Map<String, String> map);

    /**
     * 微信h5支付
     *
     * @param wxRes
     * @return
     */
    String wxH5PayBack(String wxRes);

    /**
     * 微信h5支付(充值)
     *
     * @param wxRes
     * @return
     */
    String wxH5PhonePayBack(String wxRes);

    Boolean phoneBillComplete(Integer orderId);

    /**
     * 更新支付宝的订单信息
     *
     * @param response
     */
    void updateAliPayOrdersPay(AlipayTradeQueryResponse response);

    /**
     * 获取订单列表
     *
     * @param ordersRequest
     * @return
     */
    List<OrderResponse> getOrdersList(QueryOrdersRequest ordersRequest);

    /**
     * 根据手机号获取订单列表
     * @param ordersRequest
     * @return
     */
    List<OrderResponse> queryOrdersListByUserPhone(QueryOrdersRequest ordersRequest);

    OrderResponse queryOrderInfoByNo(String childOrderNo, String source, UsersResponse usersResponse);

    /**
     * 获取订单详情列表
     *
     * @param orderId
     * @return
     */
    List<OrderInfoResponse> getOrderInfoList(Integer orderId,Integer ordersInfoId);

    /**
     * 获取积分订单列表
     *
     * @param id
     * @return
     */
    List<OrderInfoResponse> getScoreOrderInfoList(Integer id);

    /**
     * 获取积分订单列表
     *
     * @param h5Token
     * @return
     */
    List<OrderInfoResponse> getScoreOrderInfoList(String h5Token);

    /**
     * 获取新品试用订单列表
     *
     * @param id
     * @return
     */
    List<OrderInfoResponse> getNewProOrderInfoList(Integer id);

    /**
     * 动态订单列表
     *
     * @param id
     * @return
     */
    List<DynamicOrderResponse> getDynamicOrderList(Integer id, Integer pageNum, Integer pageSize);


    /**
     * 我的订单列表
     *
     * @param id
     * @return
     */
    List<OrderInfoResponse> getDynamicOrderListV2(Integer id, Integer pageNum, Integer pageSize);


    /**
     * 根据id 获取订单信息
     *
     * @param id
     * @return
     */
    OrderInfoResponse getOrderInfoById(Integer id);

    OrdersInfo getOrdersInfoById(Integer id);
    OrdersInfo getOrdersInfoByExpressNo(String expressNo);

    /**
     * 更新订单信息
     *
     * @param orderInfo
     */
    void updateOrderInfo(OrderInfoResponse orderInfo);

    String wxAppletPayBack(String resXml,String type,String tradeType);

    OrderResponse anewBuy(Integer orderId, Integer id,String channel);

    /**
     * 查询物流信息
     *
     * @param orderInfoId
     * @return
     */
    LogisticsResponse checkLogistics(Integer orderInfoId, UsersResponse currentUser);

    /**
     * 根据手机号获取订单列表
     *
     * @param ordersRequest
     * @return
     */
    List<OrderResponse> getOrdersListByTel(QueryOrdersRequest ordersRequest);

    /**
     * h5 创建订单
     *
     * @param request
     * @return
     */
    Integer h5CreateOrder(H5CreateOrderRequest request);

    String updateOrderRedpacket(String orderNo);

    /**
     * 检查支付通道是否异常
     * @param platform "alipay" 支付宝支付，"wx" 微信支付
     * @return
     */
    void checkPaymentChannelStatus(String platform,String domainUrl);

    void checkPaymentChannelStatus(String platform);


    /**
     * h5 创建订单
     *
     * @param request
     * @return
     */
    Integer h5CartCreateOrder(H5CartCreateOrderRequest request);

    /**
     * 获取商品的好评率
     */
    String getProductRatio(Product product);

    /**
     * 获取h5订单信息
     *
     * @param orderId
     * @return
     */
    OrderResponse getH5OrderInfoById(Integer orderId, UsersResponse h5CurrentUser);

    OrderResponse getH5OrderInfoByOrderNo(String orderNo, UsersResponse h5CurrentUser);

    boolean updateGoupBuyUserRelStatus(String orderNo, Integer status);

    /**
     * h5微信发起预付款订单
     *
     * @param request
     * @param usersResponse
     * @return
     */
    Object h5WxDoUnifiedOrder(UnifiedOrderRequest request,UsersResponse usersResponse);

//    /**
//     * Adapay 发起微信小程序预下单
//     *
//     * @param request
//     * @param usersResponse
//     * @return
//     */
//    Object adapayWxDoUnifiedOrder(UnifiedOrderRequest request,UsersResponse usersResponse);


    Orders getByOrderNo(String orderNo);

    /**
     * h5微信发起预付款（话费）
     * @param request
     * @param usersResponse
     * @return
     */
    Object h5WxDoUnifiedPhoneOrder(UnifiedOrderPhoneRequest request,UsersResponse usersResponse);

    /**
     * 更新h5订单支付
     *
     * @param map
     */
    void updateH5OrderPay(Map<String, String> map);

    /**
     * 更新h5订单支付(话费充值)
     *
     * @param map
     */
    void updateH5PhoneOrderPay(Map<String, String> map);


    void updateH5ISVPhoneOrderPay(WxPayOrderNotifyV3Result.DecryptNotifyResult notifyResult);

    OrderResponse getH5OrdersPayStatus(Integer orderId, Integer payOrderId);

    String handleOrderBack(final WXPay wxpay, String resXml, String type);

    String handlePhoneOrderBack(final WXPay wxpay, String resXml, String type);

    Integer getOrdersInfoCount(Integer userId);

    /**
     * 获取h5订单列表
     * @param ordersRequest
     * @return
     */
    List<OrderResponse> getH5OrdersList(QueryOrdersRequest ordersRequest);

    /**
     * 写订单地址
     * @param request
     */
    void writeOrderAddress(WriteOrderAddressRequest request);

    void writeOrderAddressV2(WriteOrderAddressRequest request);


    /**
     * 微信统一下单接口
     * @return
     */
    Map unifiedPay(String resOrderNo,String amount,String openid,String type);

    Map appRecharge(UnifiedOrderPhoneRequest request, UsersResponse usersResponse);

    String wxAppRechargePayBack(String resXml);

    String wxAppletRechargePayBack(String resXml);

    void cashOnDelivery(UnifiedOrderRequest request);

    Orders getById(Integer orderId);

    Integer selectByorderNo(String orderNo);

    Integer queryOrderNumByDynamicId(List<Integer> dynamicIds, Integer type);

    int selectOrdersZhuanShu(Integer userId);

    long helpWorkNum(Integer userId);

    CommonResult cancelOrder(String orderNo);

    CommonResult deleteOrder(Integer orderInfoId);

    void ordersUpload(Integer orderId,Integer transferId);

    boolean checkProductStock(Integer productId,Integer productSpecId,Integer productSum);

    void ProductStockFrozen(Integer orderId);

    FreightTemplateResponse getFreightTemplateResponse(Integer productId);

    String wxV3PayBack(String type, String data, SignatureHeader signatureHeader) throws WxPayException;

    /**
     * h5的Adapay 小程序支付回调通知处理
     */
    String adapayPayNotify(HttpServletRequest request,String type);

    /**
     * 申请交易账单
     * @return
     */
    String tradebillByWx() throws WxPayException;

    /**
     * 下载对账单
     * @param servletResponse
     * @throws Exception
     */
    void downloadBill(HttpServletResponse servletResponse) throws Exception;

    /**
     * 支付成功后扣库存
     * @param orderId
     */
    void subStock(Integer orderId);

    /**
     * 查询活动销量
     * @param activityType
     * @return
     */
    Integer sumOrderVolumeByActivityType(String activityType);

    String wxV3RefundBack(String type, String data, SignatureHeader signatureHeader) throws WxPayException;

    /**
     * 订单是否支付成功
     * @param orderId
     * @return CommonResult
     */
    CommonResult checkWxMiniPayStatus(Integer orderId);

    /**
     * 根据h5token查询用户未支付且未取消的订单数
     * @param h5Token
     * @return
     */
    Integer queryNoPayAndNoCancelCount(String h5Token);

    void updateOrderAddress(String childOrderNo,String tel,String address,String newTel,String userName);
}
